package com.eastfair.imaster.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.eastfair.boot.service.SuperCacheServiceImpl;
import com.eastfair.cache.exhibitor.ExhibitorProductCacheKeyBuilder;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.model.CacheKeyBuilder;
import com.eastfair.imaster.dao.ExhibitorProductMapper;
import com.eastfair.imaster.entity.ExhibitorProduct;
import com.eastfair.imaster.service.ExhibitorProductService;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 业务实现类
 * 产品表
 * </p>
 *
 * @author liuxiaobai
 * @date 2021-08-24
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class ExhibitorProductServiceImpl extends SuperCacheServiceImpl<ExhibitorProductMapper, ExhibitorProduct> implements ExhibitorProductService {

    @Autowired
    private ProjectCoreServiceFeign projectCoreServiceFeign;

    @Override
    protected CacheKeyBuilder cacheKeyBuilder() {
        return new ExhibitorProductCacheKeyBuilder();
    }

    @Override
    protected R<Boolean> handlerSave(ExhibitorProduct model) {
        //项目id
        model.setProjectId(ContextUtil.getProjectId());
        //雪花ID
        model.setId(DefaultUidGenerator.getUidOfProjectId(model.getProjectId()));
        return R.successDef();
    }

    /**************sharding 分库分表操作 *****************/
    @Override
    public boolean updateByIdOfShard(ExhibitorProduct entity) {
        UpdateWrapper<ExhibitorProduct> updateWrapper = new UpdateWrapper<>();
        //可将指定字段更新为null
        updateWrapper.lambda().eq(ExhibitorProduct::getProjectId, entity.getProjectId()).eq(ExhibitorProduct::getId, entity.getId());
        entity.setProjectId(null);
        //删除缓存
        delCache(entity);
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }


    @Override
    public void upsert(List<ExhibitorProduct> exhibitorProductList) {
        for (ExhibitorProduct product : exhibitorProductList) {
            if (product.getId() != null) {
                //主键不为空 直接update
                updateByIdOfShard(product);
            } else {
                //主键为空 先根据projectId、exhibitionId、productId查询是否同步过
                QueryWrapper<ExhibitorProduct> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda()
                        .eq(ExhibitorProduct::getProjectId, ContextUtil.getProjectId())
                        //todo 展会ID放header
                        .eq(ExhibitorProduct::getExhibitionId, product.getExhibitionId())
                        .eq(ExhibitorProduct::getProductId, product.getProductId())
                        .eq(ExhibitorProduct::getIsDeleted, BusinessConstant.DELETE_NO);
                ExhibitorProduct exhibitorProduct = getOne(queryWrapper);
                if (exhibitorProduct != null) {
                    //已存在 update
                    product.setId(exhibitorProduct.getId());
                    updateByIdOfShard(product);
                } else {
                    save(product);
                }
            }
        }
    }
}
